<!--vue模板   closeOnClickModal 设置为false ，点击遮罩时不会关闭dialog ，否则会关闭-->
<template>
  <el-dialog
    v-model="visible"
    :closeOnClickModal="false"
    width="35%"
    :title="title"
    @close="close"
  >
    <el-form label-width="120px" :model="menu" :rules="rules" ref="suform">
      <el-form-item label="类型">
        <el-radio-group v-model="menu.type" class="ml-4" :disabled="menu.id">
          <el-radio :label="1" size="large">模块</el-radio>
          <el-radio :label="2" size="large">页面</el-radio>
          <el-radio :label="3" size="large">按钮</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="name" prop="name">
        <el-input v-model="menu.name" />
      </el-form-item>

      <el-form-item label="父组件" :class="{hid:menu.type==1}">
          <MenuTree v-model:parentid="menu.parentid"></MenuTree>
      </el-form-item>

      <el-form-item label="图标" :class="{hid:menu.type==3}">
        <el-input v-model="menu.icon" />
      </el-form-item>

      <el-form-item label="路径" :class="{hid:menu.type!=2}">
        <el-input v-model="menu.path" />
      </el-form-item>

      <el-form-item label="组件" :class="{hid:menu.type!=2}">
        <el-input v-model="menu.component" />
      </el-form-item>

      <el-form-item label="授权" :class="{hid:menu.type!=3}">
        <el-input v-model="menu.auths" />
      </el-form-item>

      <el-form-item label="排序" :class="{hid:menu.type==3}">
        <el-input-number v-model="menu.sorts" :min="0" />
      </el-form-item>

      <el-form-item label="是否隐藏" :class="{hid:menu.type!=2}">
        <el-radio-group v-model="menu.ishidden" class="ml-4">
          <el-radio :label="0" size="large">否</el-radio>
          <el-radio :label="1" size="large">是</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <div style="text-align: center">
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </el-dialog>
</template>

<!--vue 组件选项定义-->
<script>
import MenuTree from "./MenuTree.vue";
export default {
  data() {
    return {
      menu: {
        name: "",
        type: 1,
        icon: "",
        path: "",
        component: "",
        ishidden: 0,
        sorts: 0,
        auths: "",
        parentid: "",
      },
      pid: "",
      rules: {
        name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
      },
    };
  },
  components: {
    MenuTree,
  },
  computed: {
    title() {
      if (this.menu.id) {
        return "修改菜单";
      }
      return "添加菜单";
    },
    name() {
      if (this.menu.type == 1) {
        return "模块名称";
      } else if (this.menu.type == 2) {
        return "页面名称";
      } else {
        return "按钮名称";
      }
    },
  },

  emits: ["update:visible", "refreshData"],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },

  methods: {
    init(data) {
      if (data == undefined) {
        //说明用户执行的添加
        this.menu = {
          name: "",
          type: 1,
          icon: "",
          path: "",
          component: "",
          ishidden: 0,
          sorts: 0,
          auths: "",
          parentid: "",
        };
      } else {
        //修改
        this.menu = data;
        this.menu.ishidden=parseInt(data.ishidden)
      }
    },

    save() {
      this.$refs.suform.validate((valid) => {
        if (!valid) return;
        let promise;
        if (this.menu.id) {
          //id存在 说明是修改
          promise = this.$http.put("/api/menu/update", this.menu);
        } else {
          promise = this.$http.post("/api/menu/save", this.menu);
        }
        promise.then((res) => {
          if (res.code == 200) {
            this.$message.success("保存成功");
            //关闭弹框
            this.close();

            //通知父组件刷新数据
            this.$emit("refreshData");
          }
        });
      });
    },

    close() {
      this.$emit("update:visible", false);
    },
  },
};
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.hid {
  display: none;
}
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
  border: 1px dashed var(--el-border-color);
}
</style>
